package org.istt.web;

import java.util.Set;

import org.istt.domain.Department;
import org.istt.domain.Field;
import org.istt.domain.User;
import org.istt.service.DepartmentManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 *
 */
public class DepartmentAction extends BaseAction<DepartmentManager, Department, Long> {
	private Department department;
	private DepartmentManager depMrg;
	
	
	public DepartmentManager getDepMrg() {
		return depMrg;
	}
	@Autowired
	public void setDepMrg(DepartmentManager depMrg) {
		this.depMrg = depMrg;
	}

	private Long id;
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	private String cancel;
	private String delete;
	private String search;
	
	/**
	 * @return the search
	 */
	public String getSearch() {
		return search;
	}

	/**
	 * @param search the search to set
	 */
	public void setSearch(String search) {
		this.search = search;
	}
	
	public String getCancel() {
       return this.cancel;
    }
	
	public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }
    
    public String getDelete() {
		return delete;
	}
	@Override
	public String execute() {
        if (cancel != null) {
            return "cancel";
        }
        
        if (delete != null) {
            return delete();
        }
        
        if (search != null) {
            return "search";
        }
        
        return SUCCESS;
    }
	
	public void setId(Long id) {
		this.id = id;
	}
	private String depName;
	public String getDepName() {
		if (entity!=null) {
			return entity.getName();
		}
		return depName;
	}
	
	//Added by NDTHO.2011.12.09
	@Override
	public void validate() {
		boolean result = ((DepartmentManager)mgr).isUnique(new String[]{"name"}, new String[]{department.getName()}, department.getId());
			if (!result) {
				addActionError(getText("department.name.existed"));
			}
	}
	
	@Override
	public String delete() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'delete' method");
        }
		if (getId() != null) {
			Department obj = mgr.get(getId());
			Set<Field> fields = obj.getFields();
			Set<User> users = obj.getUsers();
			if((fields!=null && fields.size()!=0) || (users!=null && users.size()!=0)){
				if(fields!=null && fields.size()!=0)
					ActionContext.getContext().getSession().put("message", getText("department.delete.error2"));

				if(users!=null && users.size()!=0)
					ActionContext.getContext().getSession().put("message", getText("department.delete.error1"));
				
				
				log.debug("entering 'delete error'");			
			}else{
				log.debug("entering 'delete successful'");			
				mgr.remove(getId());
				setDeleteMessage();
			}
		}else{
			log.debug("entity is null");
		}
		
        return "delete";
    }
	
	@Override
	public Long getId() {
/*		Long id = NumberUtils.toLong(department.getId());
		if (id == 0) return null;
		return id;*/
		return (department==null? id : department.getId());//id
	}


	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("department.saved"));
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("department.deleted"));
	}


	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }
        
        //entity = getEntity();
        ((DepartmentManager)mgr).save(department);
        
        setSaveMessage();
                
        return SUCCESS;
	}

    
	public void setDepartment(Department department) {
		this.department = department;
	}

	public Department getDepartment() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return department;
	}

}
